const svc = require('../util/service'),
urlDao = require('../dao/urlDao'),
wordDao = require('../dao/wordDao'),
uwDao = require('../dao/urlWordDao');

module.exports = (req, res) =>
svc.handlePost(req, res, (reqJSON, resolve) => {
	if (!reqJSON.url) {
		resolve({
			result : -1,
			msg : '缺少url'
		})
		return
	}
	if (!reqJSON.word) {
		resolve({
			result : -1,
			msg : '缺少word'
		})
		return
	}
	reqJSON.word = reqJSON.word.replace(/[^\u4e00-\u9fa5\w\d]/gi, '').toLowerCase();
	var p0 = new Promise(resolve => {
			urlDao.findByUrl(reqJSON.url, url => {
				if (!url) {
					resolve(0)
					return
				}
				resolve(url.urlId)
			})
		}),
	p1 = new Promise(resolve => {
			wordDao.findByWord(reqJSON.word, word => {
				if (!word) {
					resolve(0)
					return
				}
				resolve(word.wordId)
			})
		});
	Promise.all([p0, p1]).then(result => {
		uwDao.delete (result[0], result[1], () => {
			resolve({
				result : 1
			})
		})
	})
})
